﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class pointsForInfoHandler : IHttpHandler, IRequiresSessionState
    {

        // Fields
        

        tbPointsForOrder oPointsForOrder = new tbPointsForOrder();
        tbUserAddress oUserAddress = new tbUserAddress();


        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members


        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (((context.Request.Form["userAddress"] != null) && (context.Request.Form["postscript"] != null)) && (context.Request.Form["id"] != null))
            {
                if (Convert.ToInt32(context.Request.Form["userAddress"]) == 0)
                {
                    if (Convert.ToInt32(context.Request.Form["isSave"]) == 1)
                    {
                        this.oUserAddress.user = (context.Session["user"] as tbUser).id;
                        this.oUserAddress.consignee = context.Request.Form["consignee"];
                        this.oUserAddress.region = context.Request.Form["provinces"] + ((context.Request.Form["city"] == "") ? "" : ("," + context.Request.Form["city"]));
                        this.oUserAddress.address = context.Request.Form["address"];
                        this.oUserAddress.zipcode = context.Request.Form["zipCode"];
                        this.oUserAddress.telephone = context.Request.Form["telephone"];
                        this.oUserAddress.mobile = context.Request.Form["mobile"];
                        this.oUserAddress.isDefault = false;
                        BaseFactory.theUserAddress.Add(this.oUserAddress);
                        this.oPointsForOrder.consignee = context.Request.Form["consignee"];
                        this.oPointsForOrder.region = context.Request.Form["provinces"] + "," + context.Request.Form["city"];
                        this.oPointsForOrder.address = context.Request.Form["address"];
                        this.oPointsForOrder.zipcode = context.Request.Form["zipCode"];
                        this.oPointsForOrder.telephone = context.Request.Form["telephone"];
                        this.oPointsForOrder.mobile = context.Request.Form["mobile"];
                    }
                }
                else
                {
                    oUserAddress = BaseFactory.theUserAddress.GetModel(Convert.ToInt32(context.Request.Form["userAddress"]));
                    if (oUserAddress != null)
                    {
                        this.oPointsForOrder.consignee = oUserAddress.consignee;
                        this.oPointsForOrder.region = oUserAddress.region;
                        this.oPointsForOrder.address = oUserAddress.address;
                        this.oPointsForOrder.zipcode = oUserAddress.zipcode;
                        this.oPointsForOrder.telephone = oUserAddress.telephone;
                        this.oPointsForOrder.mobile = oUserAddress.mobile;
                    }
                }
                tbPointsFor oPointsFor = BaseFactory.thePointsFor.GetModel(Convert.ToInt32(context.Request.Form["id"]));
                this.oPointsForOrder.orderNo = DateTime.Now.ToString("yyyyMMddHHmmssffffff");
                this.oPointsForOrder.orderUser = (context.Session["user"] as tbUser).id;
                this.oPointsForOrder.orderStatus = 0;
                this.oPointsForOrder.deliveryStatus = 0;
                this.oPointsForOrder.payStatus = 0;
                this.oPointsForOrder.postscript = context.Request.Form["postscript"];
                this.oPointsForOrder.orderIntegral = oPointsFor.exchangePoints;
                this.oPointsForOrder.goodsEntitys = Convert.ToInt32(context.Request.Form["id"]);
                this.oPointsForOrder.amount = 1;

                if (BaseFactory.thePointsForOrder.Add(this.oPointsForOrder) > 0)
                {
                    context.Response.Write(this.oPointsForOrder.orderNo);
                }
                else
                {
                    context.Response.Write("0");
                }
            }
        }

        #endregion
    }
}
